<template>
	<view>
		<image src="../../../客服微信.jpg"></image>
	</view>
</template>

<script>
	import {afterExchange,orderInfo,afterInfo} from '@/common/http.api.js'
	import {isEmpty} from '@/utils/utils';
	import config from '@/common/config';
	export default {
		data() {
			return {
				mode:0,
				edit:1,
				editFlag:true,
				id:'',
				submitText:'',
				flawText:'',
				qualityText:'',
				applysuccessText:'',
				dislikeText:'',
				applyfailText:'',
				inconsistentText:'',
				describetipText:'',
				reasonText:'',
				describeText:'',
				chooseText:'',
				uploadloadingText:'',
				showReason:false,
				reasonTitle:this.$t('exchange.reason.text'),
				reasonDataList:[
					{name:this.$t('exchange.dislike.text'),type:1},
					{name:this.$t('exchange.inconsistent.text'),type:2},
					{name:this.$t('exchange.flaw.text'),type:3},
					{name:this.$t('exchange.quality.text'),type:4},
				],
				fileList: [],
				orderData:{},
				form:{
					orderId:'',
					orderGoodId:'',
					type:'',
					description:'',
					imgs:[]
				},
			};
		},
	    async onLoad(e){
			if(e.id){
				this.id = e.id;
				this.mode = parseInt(e.mode);
				this.edit = parseInt(e.edit);
				this.editFlag = false;
				await this.getAfterInfo();
			}else{
				this.form.orderId = e.orderId;
				this.form.orderGoodId = e.orderGoodId;
				await this.getOrderInfo()
			}
		},
		onShow() {
		   this.refresh();	
		},
		methods:{
			refresh(){
				this.submitText = this.$t('exchange.submit.text');
				this.flawText = this.$t('exchange.flaw.text');
				this.qualityText = this.$t('exchange.quality.text');
				this.applysuccessText = this.$t('exchange.applysuccess.text');
				this.dislikeText = this.$t('exchange.dislike.text');
				this.applyfailText = this.$t('exchange.applyfail.text');
				this.inconsistentText = this.$t('exchange.inconsistent.text');
				this.describetipText = this.$t('exchange.describetip.text');
				this.reasonText = this.$t('exchange.reason.text');
				this.describeText = this.$t('exchange.describe.text');
				this.chooseText = this.$t('common.choose.text');
				this.uploadloadingText = this.$t('common.uploadloading.text');
			},
			getOrderInfo(){
				orderInfo({params:{id:this.form.orderId}}).then(res => {
					if (res.code === 0) {
						debugger
						this.orderData = res.data;
					} else {
						uni.hideLoading();
						this.$refs.uToast.show({
							message: res.msg,
							type: 'error',
						})
					}
				});
			},
			submitForm(){
				let that = this;
				this.form.imgs=[];
				for (let i = 0; i < this.fileList.length; i++) {
					let item = this.fileList[i];
					debugger
					this.form.imgs.push(item.url);
				}
				afterExchange(this.form).then(res => {
					if (res.code === 0) {
						this.$refs.uToast.show({
							message: that.applysuccessText,
							type: 'success'
						})
						uni.hideLoading();
						setTimeout(() => {
							uni.navigateBack({
								delta: 2
							});
						}, 1000);
					} else {
						uni.hideLoading();
						uni.showModal({
							showCancel: false,
							title: that.applyfailText,
							content: res.msg,
						});
					}
				});
			},
			getAfterInfo(){
				  afterInfo({params:{id:this.id}}).then(res => {
						if (res.code === 0) {
							this.form = res.data;
							for (let i = 0; i < this.form.imgs.length; i++) {
								let imgUrl = this.form.imgs[i];
								this.fileList.push( {
									status: 'success',
									message: '',
									url: imgUrl
								});
							}
						} else {
							uni.hideLoading();
							this.$refs.uToast.show({
								message: res.msg,
								type: 'error',
							})
						}
					});
			},
			selectReasonClose(){
				this.showReason = false;
			},
			selectReasonClick(index){
				this.form.reason = index.name;
				this.form.type = index.type;
				this.showReason = false;
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				let that = this;
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: that.uploadloadingText
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let json = JSON.parse(result);
					if(json.code === 0){
						let item = this.fileList[fileListLen]
						this.fileList.splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: json.data
						}))
						fileListLen++
					}
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: config.baseUrl + '/app/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							"tenant-id": config.tenantId //token校验
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						},
						complete: (res)=>{
							let json = JSON.parse(res.data);
							if(json.code != 0){
								this.fileList.splice(this.fileList.length-1, 1)
								this.$refs.uToast.show({
									message: json.msg,
									type: 'error',
								})
							}
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
.goshop-list-item-right{
	align-items: center;
	display: flex;
}
/deep/.goshop-list-wrap .goshop-list-item-bottom{
	border-bottom:none;
}
.required{
	width: 20rpx;
    height: 20rpx;
    display: inline-block;
    vertical-align: middle;
    text-align: center;
    line-height: 40rpx;
    font-size: 40rpx;
    color: #f00;
}
.goshop-list-item-left{
	display: flex;
	align-items: center;
}
.goshop-exchange-container{
	.goshop-exchange-head{
		padding: 10rpx;
		.goshop-list-wrap{
			border-radius: 20rpx;
		}
	}
	.goshop-exchange-body{
		padding: 10rpx;
		.goshop-exchange-body-box{
			background-color: $goshop-color-auxiliary-one;
			padding: 20rpx;
			border-radius: 20rpx;
		}
	}
	.goshop-exchange-footer{
		padding: 20rpx;
	}
}
</style>
